package horsechess.client.data;

import horsechess.client.handle.Dice;
import horsechess.util.Point;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version 2.2
 * @created 17-Apr-2011 11:50:28 AM
 */
public class ChessBoardData {

    /**
     * players are current playing the game
     */
    private int currentPlayer;
    /**
     * Number of players: the number depicts players in game
     */
    private int numberPlayers;
    /**
     * Number of cell that a board content
     */
    private int numberCellBoard;
    /**
     * url of image in the src folder
     */
    private String imageBoard;
    /**
     * Array of cell on the board
     */
    public int[] arrChessBoard;
    /**
     * Number of dice on board
     */
    private int diceNumber;
    /**
     * Value of the 2nd dice
     */
    private int diceValue2;
    /**
     * the value of the 1st dice
     */
    private int diceValue1;
    private int finalValue1;
    private int finalValue2;
    /**
     * The array of horse in cage which used to store position point of horse in cage
     */
    public List<Point> positionInCages;
    /**
     * number of players
     */
    public List<PlayerData> players;

    public ChessBoardData() {
        numberCellBoard = 56;
        numberPlayers = 4;
        currentPlayer = 1;
        positionInCages = new ArrayList<Point>();
        players = new ArrayList<PlayerData>();
        imageBoard = "resource/chessboard/ChessBoard.png";
        initArrChessBoard();
        init();

    }

    /**
     *
     * @param currentPlayer
     * @param numOfPlayer
     */
    public ChessBoardData(int currentPlayer, int numOfPlayer) {
        numberCellBoard = 56;
        numberPlayers = numOfPlayer;
        this.currentPlayer = currentPlayer;
        positionInCages = new ArrayList<Point>();
        players = new ArrayList<PlayerData>(numOfPlayer);
        imageBoard = "/resource/chessboard/ChessBoard.png";
        initArrChessBoard();
        init();
    }

    /**
     *
     * @param data
     */
    public ChessBoardData(CustomizeData data) {
        numberCellBoard = 56;
        numberPlayers = data.getNumberOfPlayer();
        currentPlayer = data.getPriorityPerson();
        positionInCages = new ArrayList<Point>();
        players = new ArrayList<PlayerData>(numberPlayers);
        imageBoard = "/resource/chessboard/ChessBoard.png";
        initArrChessBoard();
        init(data.getNumberChessMan(), data.playerNames);
    }

    /**
     * Initialize array of position of final when house joined into those final cell
     *
     * @param user    The info of player who play that team
     * @param iUser    number that mark player team
     */
    public void initFinalPositon(int iUser, PlayerData user) {
        Point p;
        int d = 42;
        int n = 6;
        int t;
        if (iUser == 0) {
            //Position in cage of player one
            p = new Point(305, 560);
            for (int i = 0; i < n; i++) {
                t = p.getY() + (-d * i);
                Point temp = new Point(p.getX(), t);
                user.listOfFinalPosition.add(temp);
                //System.out.println("Horse 1 in cage at: "+temp.toString());
            }
        }
        if (iUser == 1) {
            //Position in cage of player two
            p = new Point(555, 308);
            for (int i = 0; i < n; i++) {
                t = p.getX() + (-d * i);
                Point temp = new Point(t, p.getY());
                user.listOfFinalPosition.add(temp);
                // System.out.println("Horse 2 in cage at: "+temp.toString());
            }
        }
        if (iUser == 2) {
            //Position in cage of player Three
            p = new Point(305, 50);
            for (int i = 0; i < n; i++) {
                t = p.getY() + (d * i);
                Point temp = new Point(p.getX(), t);
                user.listOfFinalPosition.add(temp);
                //  System.out.println("Horse 3 in cage at: "+temp.toString());
            }
        }
        if (iUser == 3) {
            p = new Point(50, 307);
            for (int i = 0; i < n; i++) {
                t = p.getX() + d * i;
                Point temp = new Point(t, p.getY());
                user.listOfFinalPosition.add(temp);
                // System.out.println("Horse 4 in cage at: "+temp.toString());
            }
        }
    }

    /**
     * Initialize value of house in cage
     */
    public void initPositionInCage() {
        Point temp = new Point(432, 610);
        positionInCages.add(temp);
        temp = new Point(432, 5);
        positionInCages.add(temp);
        temp = new Point(175, 5);
        positionInCages.add(temp);
        temp = new Point(180, 605);
        positionInCages.add(temp);
    }

    /**
     * Initialize number of cell on the board
     */
    public void initArrChessBoard() {
        arrChessBoard = new int[numberCellBoard];
        for (int i = 0; i < numberCellBoard; i++) {
            arrChessBoard[i] = 0;
        }
    }

    /**
     * - Initialize number of horse in the cage
     * - Initialize position of horses in cage
     *
     * @param numOfHorse    The number of horse will be play on the board
     */
    public void init(int[] arrNumOfChessman, String[] playerNames) {
        for (int i = 0; i < numberPlayers; i++) {
            PlayerData tam = new PlayerData((int) arrNumOfChessman[i]);
            tam.setUsername(playerNames[i]);
            tam.setScore(0);
            initFinalPositon(i, tam);
            players.add(tam);
        }
        initPositionInCage();
    }

    public void init() {
        for (int i = 0; i < numberPlayers; i++) {
            PlayerData tam = new PlayerData(4);
            //tam.setUsername("User "+(i+1)+"");
            initFinalPositon(i, tam);
            players.add(tam);

        }
        initPositionInCage();
    }

    /**
     * Update and refresh data on board.
     * Clear list of user
     * Clear list position of horse in cage
     * Assign info from customize data instance to associate info on
     * chessboard(NumberPlayers, currentPlayer, starBattleValue1, startBattleValue2,
     * finalValue1, finalValue2)
     * Initialize chessboard with number of chessboard
     *
     * @param tc
     */
    public void updateData(CustomizeData tc) {
        players.clear();
        positionInCages.clear();
        this.numberPlayers = tc.getNumberOfPlayer();
        this.currentPlayer = tc.getPriorityPerson();
        this.finalValue1 = tc.getFinalValue1();
        this.finalValue2 = tc.getFinalValue2();
        this.init(tc.getNumberChessMan(), tc.playerNames);
    }

    /**
     *
     * @param dice
     */
    public void updateDiceValue(Dice dice) {
        diceNumber = dice.getNumberDices();
        if (this.diceNumber == 1) {
            this.diceValue1 = dice.getDiceValue1();
            //System.out.println("Dice value: "+diceValue1);
        } else {
            this.diceValue1 = dice.getDiceValue1();
        }
        this.diceValue2 = dice.getDiceValue2();
    }

    public PlayerData getPlayerHighestScore() {
        boolean isDraw = true;
        PlayerData playerLargestScore = players.get(0);
        for (int i = 1; i < players.size(); i++) {
            PlayerData pd = players.get(i);
            if (pd.getScore() > playerLargestScore.getScore()) {
                playerLargestScore = pd;
            }
        }

        for (int i = 1; i < players.size(); i++) {
            PlayerData pd = players.get(i);
            if (pd.getScore() != playerLargestScore.getScore()) {
                isDraw = false;
            } else {
                isDraw=true;
            }
        }
        if (!isDraw) {
            System.out.println("The player got highest score is: " + playerLargestScore.getUsername());
            return playerLargestScore;
        } else {
           return null;
        }
    }

    /**
     * return value of dices on the board
     */
    public int getDiceValue() {
        if (diceNumber == 1) {
            return diceValue1;
        } else {
            return diceValue1 + diceValue2;
        }
    }

    /**
     * Getter && Setter
     */
    public int getCurrentPlayer() {
        return currentPlayer;
    }

    public void setCurrentPlayer(int currentPlayer) {
        this.currentPlayer = currentPlayer;
    }

    public String getImageBoard() {
        return imageBoard;
    }

    public void setImageBoard(String imageBoard) {
        this.imageBoard = imageBoard;
    }

    public int getNumberCellBoard() {
        return numberCellBoard;
    }

    public void setNumberCellBoard(int numberCellBoard) {
        this.numberCellBoard = numberCellBoard;
    }

    public int getNumberPlayers() {
        return numberPlayers;
    }

    public void setNumberPlayers(int numberPlayers) {
        this.numberPlayers = numberPlayers;
    }

    public int getDiceNumber() {
        return diceNumber;
    }

    public void setDiceNumber(int diceNumber) {
        this.diceNumber = diceNumber;
    }

    public int getDiceValue1() {
        return diceValue1;
    }

    public void setDiceValue1(int diceValue1) {
        this.diceValue1 = diceValue1;
    }

    public int getDiceValue2() {
        return diceValue2;
    }

    public void setDiceValue2(int diceValue2) {
        this.diceValue2 = diceValue2;
    }

    public int getFinalValue1() {
        return finalValue1;
    }

    public void setFinalValue1(int finalValue1) {
        this.finalValue1 = finalValue1;
    }

    public int getFinalValue2() {
        return finalValue2;
    }

    public void setFinalValue2(int finalValue2) {
        this.finalValue2 = finalValue2;
    }
}
